package net.googlecode.morenko.task5;

import net.googlecode.morenko.task5.workers.Worker;

import java.util.Collection;
import java.util.Comparator;
import java.util.TreeSet;

/**
 * Class for printing necessary information about workers.
 */
public class SortUtils {
    /**
     * Prints information about workers in collection workerCollection
     * sorted by personal code (workers with max and min salary are not printed here).
     * @param workerCollection collection that stores worker objects.
     */
    public static void printSortedByCode(Collection<Worker> workerCollection) {
        TreeSet<Worker> workerSet1 = new TreeSet<Worker>(new salaryComparator());
        workerSet1.addAll(workerCollection);
        workerSet1.pollFirst();
        workerSet1.pollLast();
        TreeSet<Worker> workerSet2 = new TreeSet<Worker>(new codeComparator());
        workerSet2.addAll(workerSet1);
        for (Worker x : workerSet2) {
            System.out.println(x);
        }
    }

    /**
     * Prints information about workers in collection workerCollection
     * sorted by last name (workers with age more than 45 are not printed here).
     * @param workerCollection collection that stores worker objects.
     */
    public static void printSortedByLastName(Collection<Worker> workerCollection) {
        TreeSet<Worker> workerSet = new TreeSet<Worker>(new lastNameComparator());
        workerSet.addAll(workerCollection);
        for (Worker x : workerSet) {
            if (x.getAge() <= 45) {
                System.out.println(x);
            }
        }
    }

    /**
     * Prints information about workers in collection workerCollection
     * sorted by personal salary (workers with the same salaries are not printed here).
     * @param workerCollection collection that stores worker objects.
     */
    public static void printSortedBySalary(Collection<Worker> workerCollection) {
        TreeSet<Worker> workerSet = new TreeSet<Worker>(new salaryComparator());
        workerSet.addAll(workerCollection);
        int previousSalary = 0;
        for (Worker x : workerSet) {
            if (x.getSalary() != previousSalary) {
                System.out.println(x);
            }
            previousSalary = x.getSalary();
        }
    }
}

/**
 * Class that compares workers by personal code.
 */
class codeComparator implements Comparator<Worker> {
    public int compare(Worker worker1, Worker worker2) {
        if (worker1.getCode() == worker2.getCode()) {
            return 0;
        }
        else if (worker1.getCode() < worker2.getCode()) {
            return -1;
        }
        else {
            return 1;
        }
    }
}

/**
 * Class that compares workers by salary.
 */
class salaryComparator implements Comparator<Worker> {
    public int compare(Worker worker1, Worker worker2) {
        if (worker1.getSalary() == worker2.getSalary()) {
            return 0;
        }
        else if (worker1.getSalary() < worker2.getSalary()) {
            return -1;
        }
        else {
            return 1;
        }
    }
}

/**
 * Class that compares workers by last name.
 */
class lastNameComparator implements Comparator<Worker> {
    public int compare(Worker worker1, Worker worker2) {
        return worker1.getLastName().compareTo(worker2.getLastName());
    }
}
